﻿using System;
using System.Collections.Generic;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Cms.Application.Admin.Presenters;
using Cms.Application.Admin.Views;
using Cms.Domain;
using Cms.Web.Framework.Controls;
using iLib.Web.UI;

namespace Cms.Web.UI.Admin
{
    public partial class EditLocale : AdminPage, IEditLocaleView
    {
        private EditLocalePresenter _presenter;

        #region IEditLocaleView Members

        public int LocaleID
        {
            get
            {
                if (ViewState["LocaleID"] == null)
                    ViewState["LocaleID"] = QueryString.GetInt32ID("id");
                return (int) ViewState["LocaleID"];
            }
        }


        public void SetLocaleID(int id)
        {
            ViewState["LocaleID"] = id;
        }


        public string ContextTranslationFile
        {
            get { return "Admin/Translations/EditLocale.xml"; }
        }


        public string UrlCode
        {
            get { return txtUrlCode.Text; }
            set { txtUrlCode.Text = value; }
        }


        public string AliasDomain
        {
            get { return txtAliasDomain.Text; }
            set { txtAliasDomain.Text = value; }
        }


        public string CultureCode
        {
            get { return txtCultureCode.Text; }
            set { txtCultureCode.Text = value; }
        }


        public string NameInNativeLanguage
        {
            get { return txtNameInNativeLanguage.Text; }
            set { txtNameInNativeLanguage.Text = value; }
        }


        public string RegionInNativeLanguage
        {
            get { return txtRegionInNativeLanguage.Text; }
            set { txtRegionInNativeLanguage.Text = value; }
        }


        public string LanguageNameInNativeLanguage
        {
            get { return txtLanguageNameInNativeLanguage.Text; }
            set { txtLanguageNameInNativeLanguage.Text = value; }
        }


        public void SetLocaleTranslations(IList<Locale> otherLocales)
        {
            rpThisLocaleTranslations.DataSource = otherLocales;
            rpThisLocaleTranslations.DataBind();
            rpOtherLocaleTranslations.DataSource = otherLocales;
            rpOtherLocaleTranslations.DataBind();
            pnlTranslations.Visible = true;
        }

        #endregion

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            _presenter = new EditLocalePresenter(this);
            if (LocaleID > 0)
                HeadingText = GetText(this, "EditLocaleHeading");
            else
                HeadingText = GetText(this, "NewLocaleHeading");
            if (!Page.IsPostBack)
            {
                _presenter.Initialize();
                HeadingIcon = litIcon.Text;
            }
        }


        protected void rpThisLocaleTranslations_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem)
                return;
            var otherLocale = (Locale) e.Item.DataItem;
            var litName = (Literal) e.Item.FindControl("litName");
            var litLanguage = (Literal) e.Item.FindControl("litLanguage");
            var txtName = (TextBox) e.Item.FindControl("txtName");
            var txtRegion = (TextBox) e.Item.FindControl("txtRegion");
            var txtLanguage = (TextBox) e.Item.FindControl("txtLanguage");
            var hidID = (HtmlInputHidden) e.Item.FindControl("hidID");
            litName.Text = otherLocale.GetTranslation(Localization).Name;
            litLanguage.Text = otherLocale.GetTranslation(Localization).Language;
            hidID.Value = otherLocale.ID.ToString();
            txtName.Text = _presenter.GetThisLocaleNameTranslation(otherLocale.ID);
            txtRegion.Text = _presenter.GetThisLocaleRegionTranslation(otherLocale.ID);
            txtLanguage.Text = _presenter.GetThisLocaleLanguageTranslation(otherLocale.ID);
        }


        protected void rpOtherLocaleTranslations_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem)
                return;
            var otherLocale = (Locale) e.Item.DataItem;
            var litName = (Literal) e.Item.FindControl("litName");
            var litLanguage = (Literal) e.Item.FindControl("litLanguage");
            var txtName = (TextBox) e.Item.FindControl("txtName");
            var txtRegion = (TextBox) e.Item.FindControl("txtRegion");
            var txtLanguage = (TextBox) e.Item.FindControl("txtLanguage");
            var hidID = (HtmlInputHidden) e.Item.FindControl("hidID");
            litName.Text = otherLocale.GetTranslation(Localization).Name;
            litLanguage.Text = otherLocale.GetTranslation(Localization).Language;
            hidID.Value = otherLocale.ID.ToString();
            if (otherLocale.Translations.ContainsKey(LocaleID))
            {
                txtName.Text = otherLocale.Translations[LocaleID].Name;
                txtRegion.Text = otherLocale.Translations[LocaleID].Region;
                txtLanguage.Text = otherLocale.Translations[LocaleID].Language;
            }
        }


        protected void lbSave_Click(object sender, EventArgs e)
        {
            TextBox txtName, txtRegion, txtLanguage;
            HtmlInputHidden hidID;
            LocaleTranslation translation;
            IDictionary<int, LocaleTranslation> thisLocaleTranslations = new Dictionary<int, LocaleTranslation>();
            IDictionary<int, LocaleTranslation> otherLocaleTranslations = new Dictionary<int, LocaleTranslation>();
            foreach (RepeaterItem ri in rpThisLocaleTranslations.Items)
            {
                hidID = (HtmlInputHidden) ri.FindControl("hidID");
                txtName = (TextBox) ri.FindControl("txtName");
                txtRegion = (TextBox) ri.FindControl("txtRegion");
                txtLanguage = (TextBox) ri.FindControl("txtLanguage");
                translation = new LocaleTranslation(txtName.Text, txtRegion.Text, txtLanguage.Text);
                thisLocaleTranslations.Add(int.Parse(hidID.Value), translation);
            }
            foreach (RepeaterItem ri in rpOtherLocaleTranslations.Items)
            {
                hidID = (HtmlInputHidden) ri.FindControl("hidID");
                txtName = (TextBox) ri.FindControl("txtName");
                txtRegion = (TextBox) ri.FindControl("txtRegion");
                txtLanguage = (TextBox) ri.FindControl("txtLanguage");
                translation = new LocaleTranslation(txtName.Text, txtRegion.Text, txtLanguage.Text);
                otherLocaleTranslations.Add(int.Parse(hidID.Value), translation);
            }
            _presenter.Save(thisLocaleTranslations, otherLocaleTranslations);
            if (_presenter.IsValid)
                Response.Redirect("Locales.aspx");
        }
    }
}